package com.keyancompany.zhongbaopingtai.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.MemberAdapter;
import com.keyancompany.zhongbaopingtai.api.CircleAPI;
import com.keyancompany.zhongbaopingtai.api.FriendsAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.CircleModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.EmptyView;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class CircleMemberActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    String title = "圈子成员";
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listview)
    IListView listView;
    @Bind(R.id.activity_member)
    LinearLayout activityMember;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;
    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    MemberAdapter memberAdapter;

    String circleId;
    String res;
    private int currentPage, total;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_circle_member;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
        initSwipeLayout(swipeLayout);
    }

    @Override
    protected void setUpViews() {
        res = getIntent().getExtras().getString("res");
        circleId = getIntent().getExtras().getString(Constants.TEAM_ID);
        memberAdapter = new MemberAdapter(null, this);
        listView.setAdapter(memberAdapter);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                EventBusEntity entity = new EventBusEntity(EventBusEntity.Type.REFRENCE);
//                entity.setItem((Serializable) memberAdapter.getItem(i));
//                EventBus.getDefault().post(entity);
//                finish();
                if (res.equals("MyCircleFragment"))
                    CircleAPI.upAdmin(circleId, memberAdapter.getItem(i).getUserId(), CircleMemberActivity.this, getLoading(R.string.watting_hint));
            }
        });

    }

    @OnClick(R.id.empty_hint)
    public void reqeustAgain(View v) {
        currentPage = 1;
        CircleAPI.searchMember(circleId, currentPage + "", this, null);
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        CircleAPI.searchMember(circleId, currentPage + "", this, null);
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        swipeLayout.setRefreshing(false);
        BaseModel base = validateJson(json);
        if (base != null && base.isSuccess()) {
            if (type == CircleAPI.UPADMIN_CODE) {
                EventBusEntity entity = new EventBusEntity(EventBusEntity.Type.REFRENCE);
                EventBus.getDefault().post(entity);
                finish();

            } else if (type == CircleAPI.SEARCH_REMEMBER) {
                JSONObject obj = JSONObject.parseObject(base.data);
                currentPage = obj.getInteger("pageNumber");
                total = obj.getInteger("pageCount");
                String teamusers = obj.getString("list");
                List<User> users = JSONObject.parseArray(teamusers, User.class);
                if (currentPage == 1) {
                    memberAdapter.clear();
                }
                listView.loadMoreFinish();
                if (total == currentPage) {
                    listView.removeFooterView();
                }

                if (users != null && !users.isEmpty()) {
                    swipeLayout.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                    memberAdapter.addAllAfter(users);
                } else {
                    swipeLayout.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);

                }

            }
        }
    }
}
